package diploma.kisel.modelas.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import diploma.kisel.hibernate.util.HibernateUtil;
import diploma.kisel.modelas.domain.Computer;
import diploma.kisel.modelas.domain.Concept;
import diploma.kisel.modelas.domain.Project;
import diploma.kisel.modelas.repository.api.ConceptRepository;

public class ConceptRepositoryImpl implements ConceptRepository{
	
	private EntityManager em = HibernateUtil.getEm();

	@Override
	public List<Concept> findAll() {
		List<Concept> concepts= em.createNamedQuery(
				"Concept.findAll", Concept.class).getResultList();		
		return concepts;		
	}

	@Override
	public Concept findById(int id) {
		TypedQuery<Concept> query = em.createNamedQuery("Concept.findById", Concept.class);
		query.setParameter("id", id);
		return query.getSingleResult();
	}

	@Override
	public Concept save(Concept concept) {
		em.getTransaction().begin();
		em.persist(concept);
		em.getTransaction().commit();
		return concept;
	}

	@Override
	public Concept update(Concept concept) {
		em.getTransaction().begin();
		em.merge(concept);
		em.getTransaction().commit();
		return concept;
	}

	@Override
	public void remove(Concept concept) {
		em.getTransaction().begin();
		Concept mergedConcept = em.merge(concept);
		em.remove(mergedConcept);			
		em.getTransaction().commit();			
	}

	@Override
	public List<Concept> findByProject(Project project) {
		TypedQuery<Concept> query = em.createNamedQuery("Concept.findByProject", Concept.class);
		query.setParameter("project", project);
		return query.getResultList();
	}

}
